package com.ml.mall.utils;

import java.math.BigDecimal;

/**
 * Created by kyyang on 2021/4/7 20:02
 */
public class NumberUtils {


    public static void main(String[] args) {
        double a1 = 82.01;
        double a2 = 82.02;

        double b1 = 82.03;
        double b2 = 82.04;

        double c1 = 82.06;
        double c2 = 82.07;

        double d1 = 82.08;
        double d2 = 82.09;

        System.out.println(getRoundByPenny(BigDecimal.valueOf(a1)));
        System.out.println(getRoundByPenny(BigDecimal.valueOf(a2)));

        System.out.println(getRoundByPenny(BigDecimal.valueOf(b1)));
        System.out.println(getRoundByPenny(BigDecimal.valueOf(b2)));

        System.out.println(getRoundByPenny(BigDecimal.valueOf(c1)));
        System.out.println(getRoundByPenny(BigDecimal.valueOf(c2)));

        System.out.println(getRoundByPenny(BigDecimal.valueOf(d1)));
        System.out.println(getRoundByPenny(BigDecimal.valueOf(d2)));


    }

    //结账页面算法
    public static String getRoundByPenny(BigDecimal bigDecimal) {
        //获取分值
        if(bigDecimal==null){
            return "0.00";
        }
        int i = bigDecimal.multiply(BigDecimal.valueOf(100)).setScale(0,BigDecimal.ROUND_DOWN).intValue()%10;

        switch (i){
            case 0:
            case 1:
            case 2:
                bigDecimal= bigDecimal.subtract(BigDecimal.valueOf((double)i/100));
                break;
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
                bigDecimal= bigDecimal.subtract(BigDecimal.valueOf((double)i/100)).add(BigDecimal.valueOf(0.05));
                break;
            case 8:
            case 9:
                bigDecimal= bigDecimal.subtract(BigDecimal.valueOf((double)i/100)).add(BigDecimal.valueOf(0.1));
                break;
        }
        return String.format("%.2f", bigDecimal.doubleValue());
    }



}
